{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": 11,
      "metadata": {
        "collapsed": false,
        "pycharm": {
          "is_executing": false
        }
      },
      "outputs": [
        {
          "name": "stdout",
          "text": [
            "[\u0027NDB_No\u0027, \u0027Shrt_Desc\u0027, \u0027Water_(g)\u0027, \u0027Energ_Kcal\u0027, \u0027Protein_(g)\u0027, \u0027Lipid_Tot_(g)\u0027, \u0027Ash_(g)\u0027, \u0027Carbohydrt_(g)\u0027, \u0027Fiber_TD_(g)\u0027, \u0027Sugar_Tot_(g)\u0027, \u0027Calcium_(mg)\u0027, \u0027Iron_(mg)\u0027, \u0027Magnesium_(mg)\u0027, \u0027Phosphorus_(mg)\u0027, \u0027Potassium_(mg)\u0027, \u0027Sodium_(mg)\u0027, \u0027Zinc_(mg)\u0027, \u0027Copper_(mg)\u0027, \u0027Manganese_(mg)\u0027, \u0027Selenium_(mcg)\u0027, \u0027Vit_C_(mg)\u0027, \u0027Thiamin_(mg)\u0027, \u0027Riboflavin_(mg)\u0027, \u0027Niacin_(mg)\u0027, \u0027Vit_B6_(mg)\u0027, \u0027Vit_B12_(mcg)\u0027, \u0027Vit_A_IU\u0027, \u0027Vit_A_RAE\u0027, \u0027Vit_E_(mg)\u0027, \u0027Vit_D_mcg\u0027, \u0027Vit_D_IU\u0027, \u0027Vit_K_(mcg)\u0027, \u0027FA_Sat_(g)\u0027, \u0027FA_Mono_(g)\u0027, \u0027FA_Poly_(g)\u0027, \u0027Cholestrl_(mg)\u0027]\n   NDB_No                 Shrt_Desc  Water_(g)  Energ_Kcal  Protein_(g)  \\\n0    1001          BUTTER WITH SALT      15.87         717         0.85   \n1    1002  BUTTER WHIPPED WITH SALT      15.87         717         0.85   \n2    1003      BUTTER OIL ANHYDROUS       0.24         876         0.28   \n\n   Lipid_Tot_(g)  Ash_(g)  Carbohydrt_(g)  Fiber_TD_(g)  Sugar_Tot_(g)  \\\n0          81.11     2.11            0.06           0.0           0.06   \n1          81.11     2.11            0.06           0.0           0.06   \n2          99.48     0.00            0.00           0.0           0.00   \n\n        ...        Vit_A_IU  Vit_A_RAE  Vit_E_(mg)  Vit_D_mcg  Vit_D_IU  \\\n0       ...          2499.0      684.0        2.32        1.5      60.0   \n1       ...          2499.0      684.0        2.32        1.5      60.0   \n2       ...          3069.0      840.0        2.80        1.8      73.0   \n\n   Vit_K_(mcg)  FA_Sat_(g)  FA_Mono_(g)  FA_Poly_(g)  Cholestrl_(mg)  \n0          7.0      51.368       21.021        3.043           215.0  \n1          7.0      50.489       23.426        3.012           219.0  \n2          8.6      61.924       28.732        3.694           256.0  \n\n[3 rows x 36 columns]\n"
          ],
          "output_type": "stream"
        }
      ],
      "source": [
        "import pandas\n",
        "food_info \u003d pandas.read_csv(\"food_info.csv\")\n",
        "col_names \u003d food_info.columns.tolist()\n",
        "print(col_names)\n",
        "print(food_info.head(3))"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false,
        "pycharm": {}
      },
      "outputs": [],
      "source": [
        "#print food_info[\"Iron_(mg)\"]\n",
        "#div_1000 \u003d food_info[\"Iron_(mg)\"] / 1000\n",
        "#print div_1000\n",
        "# Adds 100 to each value in the column and returns a Series object.\n",
        "#add_100 \u003d food_info[\"Iron_(mg)\"] + 100\n",
        "\n",
        "# Subtracts 100 from each value in the column and returns a Series object.\n",
        "#sub_100 \u003d food_info[\"Iron_(mg)\"] - 100\n",
        "\n",
        "# Multiplies each value in the column by 2 and returns a Series object.\n",
        "#mult_2 \u003d food_info[\"Iron_(mg)\"]*2\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 5,
      "metadata": {
        "collapsed": true,
        "pycharm": {}
      },
      "outputs": [],
      "source": [
        "#It applies the arithmetic operator to the first value in both columns, the second value in both columns, and so on\n",
        "water_energy \u003d food_info[\"Water_(g)\"] * food_info[\"Energ_Kcal\"]\n",
        "water_energy \u003d food_info[\"Water_(g)\"] * food_info[\"Energ_Kcal\"]\n",
        "\n",
        "iron_grams \u003d food_info[\"Iron_(mg)\"] / 1000\n",
        "food_info[\"Iron_(g)\"] \u003d iron_grams"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": true,
        "pycharm": {}
      },
      "outputs": [],
      "source": [
        "#Score\u003d2×(Protein_(g))−0.75×(Lipid_Tot_(g))\n",
        "weighted_protein \u003d food_info[\"Protein_(g)\"] * 2\n",
        "weighted_fat \u003d -0.75 * food_info[\"Lipid_Tot_(g)\"]\n",
        "initial_rating \u003d weighted_protein + weighted_fat"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": true,
        "pycharm": {}
      },
      "outputs": [],
      "source": [
        "# the \"Vit_A_IU\" column ranges from 0 to 100000, while the \"Fiber_TD_(g)\" column ranges from 0 to 79\n",
        "#For certain calculations, columns like \"Vit_A_IU\" can have a greater effect on the result, \n",
        "#due to the scale of the values\n",
        "# The largest value in the \"Energ_Kcal\" column.\n",
        "max_calories \u003d food_info[\"Energ_Kcal\"].max()\n",
        "# Divide the values in \"Energ_Kcal\" by the largest value.\n",
        "normalized_calories \u003d food_info[\"Energ_Kcal\"] / max_calories\n",
        "normalized_protein \u003d food_info[\"Protein_(g)\"] / food_info[\"Protein_(g)\"].max()\n",
        "normalized_fat \u003d food_info[\"Lipid_Tot_(g)\"] / food_info[\"Lipid_Tot_(g)\"].max()\n",
        "food_info[\"Normalized_Protein\"] \u003d normalized_protein\n",
        "food_info[\"Normalized_Fat\"] \u003d normalized_fat"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 17,
      "metadata": {
        "collapsed": false,
        "pycharm": {
          "is_executing": false
        }
      },
      "outputs": [
        {
          "name": "stdout",
          "text": [
            "760     0.0\n2269    0.0\n763     0.0\n770     0.0\n774     0.0\n396     0.0\n395     0.0\n701     0.0\n6827    0.0\n393     0.0\n391     0.0\n390     0.0\n787     0.0\n788     0.0\n2270    0.0\n2231    0.0\n407     0.0\n748     0.0\n394     0.0\n383     0.0\n761     0.0\n405     0.0\n699     0.0\n409     0.0\n706     0.0\n707     0.0\n730     0.0\n738     0.0\n420     0.0\n384     0.0\n       ... \n8153    NaN\n8155    NaN\n8156    NaN\n8157    NaN\n8158    NaN\n8159    NaN\n8160    NaN\n8161    NaN\n8163    NaN\n8164    NaN\n8165    NaN\n8167    NaN\n8169    NaN\n8170    NaN\n8172    NaN\n8173    NaN\n8174    NaN\n8175    NaN\n8176    NaN\n8177    NaN\n8178    NaN\n8179    NaN\n8180    NaN\n8181    NaN\n8183    NaN\n8184    NaN\n8185    NaN\n8195    NaN\n8251    NaN\n8267    NaN\nName: Sodium_(mg), Length: 8618, dtype: float64\n276     38758.0\n5814    27360.0\n6192    26050.0\n1242    26000.0\n1245    24000.0\n1243    24000.0\n1244    23875.0\n292     17000.0\n1254    11588.0\n5811    10600.0\n8575     9690.0\n291      8068.0\n1249     8031.0\n5812     7893.0\n1292     7851.0\n293      7203.0\n4472     7027.0\n4836     6820.0\n1261     6580.0\n3747     6008.0\n1266     5730.0\n4835     5586.0\n4834     5493.0\n1263     5356.0\n1553     5203.0\n1552     5053.0\n1251     4957.0\n1257     4843.0\n294      4616.0\n8613     4450.0\n         ...   \n8153        NaN\n8155        NaN\n8156        NaN\n8157        NaN\n8158        NaN\n8159        NaN\n8160        NaN\n8161        NaN\n8163        NaN\n8164        NaN\n8165        NaN\n8167        NaN\n8169        NaN\n8170        NaN\n8172        NaN\n8173        NaN\n8174        NaN\n8175        NaN\n8176        NaN\n8177        NaN\n8178        NaN\n8179        NaN\n8180        NaN\n8181        NaN\n8183        NaN\n8184        NaN\n8185        NaN\n8195        NaN\n8251        NaN\n8267        NaN\nName: Sodium_(mg), Length: 8618, dtype: float64\n"
          ],
          "output_type": "stream"
        }
      ],
      "source": "#By default, pandas will sort the data by the column we specify in ascending order and return a new DataFrame\n# Sorts the DataFrame in-place, rather than returning a new DataFrame.\n#print food_info[\"Sodium_(mg)\"]\nfood_info.sort_values(\"Sodium_(mg)\", inplace\u003dTrue)\nprint(food_info[\"Sodium_(mg)\"])\n#Sorts by descending order, rather than ascending.\nfood_info.sort_values(\"Sodium_(mg)\", inplace\u003dTrue, ascending\u003dFalse)\nprint(food_info[\"Sodium_(mg)\"])"
    },
    {
      "cell_type": "code",
      "execution_count": 18,
      "metadata": {
        "collapsed": true,
        "pycharm": {
          "is_executing": false
        }
      },
      "outputs": [
        {
          "name": "stdout",
          "text": [
            "760     0.0\n774     0.0\n396     0.0\n701     0.0\n6827    0.0\n393     0.0\n391     0.0\n705     0.0\n390     0.0\n788     0.0\n2270    0.0\n2231    0.0\n407     0.0\n748     0.0\n394     0.0\n383     0.0\n761     0.0\n405     0.0\n787     0.0\n2397    0.0\n770     0.0\n763     0.0\n758     0.0\n699     0.0\n420     0.0\n384     0.0\n739     0.0\n741     0.0\n742     0.0\n764     0.0\n       ... \n8153    NaN\n8155    NaN\n8156    NaN\n8157    NaN\n8158    NaN\n8159    NaN\n8160    NaN\n8161    NaN\n8163    NaN\n8164    NaN\n8165    NaN\n8167    NaN\n8169    NaN\n8170    NaN\n8172    NaN\n8173    NaN\n8174    NaN\n8175    NaN\n8176    NaN\n8177    NaN\n8178    NaN\n8179    NaN\n8180    NaN\n8181    NaN\n8183    NaN\n8184    NaN\n8185    NaN\n8195    NaN\n8251    NaN\n8267    NaN\nName: Sodium_(mg), Length: 8618, dtype: float64\n"
          ],
          "output_type": "stream"
        }
      ],
      "source": "\n"
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.6.5"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 2
}